package com.adgox.tiantianbiting.mine.tingCoinRecord;

import android.widget.Toast;

import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.RechargeInfoBean;
import com.adgox.tiantianbiting.bean.TradeRecordBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.mine.mobile.BindMobileActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;
import java.util.List;

/**
 * Created by tang on 2019/2/28.
 */

public class TingCoinRecordPresenterImpl implements TingCoinRecordContract.Presenter {

    TingCoinRecordContract.View mView;
    private int userId = ((UserInfo)SessionManager.getDefault().getUser()).getId();

    public TingCoinRecordPresenterImpl(TingCoinRecordContract.View mView) {
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        getExchangeRate();
    }

    private void getExchangeRate() {
        OkGo.<BaseResponseBean<List<RechargeInfoBean>>>get(ApiConstant.BASE_URL+ApiConstant.GET_RECHARGE_INFO)
                .execute(new JsonCallback<BaseResponseBean<List<RechargeInfoBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<RechargeInfoBean>>> response) {
                        if (response.body() == null || !"0".equals(response.body().getCode())){
                            mView.onsetTips1Failed("获取兑换比例失败");
                            return;
                        }
                        mView.onSetTips1(response.body().getData().get(0));
                    }
                });
    }

    @Override
    public void getRecordData(int currentPage, int maxPage) {

        OkGo.<BaseResponseBean<PageBean<TradeRecordBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_RECORD + userId + "/1")
                .params("startPoint", currentPage)
                .params("maxPage", maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<TradeRecordBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<TradeRecordBean>>> response) {
//                        onSetData(response.body().getData());
                        if (response.body() == null || !"0".equals(response.body().getCode()) || response.body().getData() == null){
                            mView.onSetRvDataFailed("获取列表数据失败");
                            return;
                        }
                        mView.onSetRvData(response.body().getData());
                    }
                });
    }

    //发送验证码
    @Override
    public void sendVert(String mobile,int userId) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL+ApiConstant.GET_VERTIFY_CODE+mobile+"/"+userId)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            mView.onGetVertSuccess();
                        } else {
                            mView.onGetVertFailed(response.body().getMessage());
                        }
                    }
                });
    }

    @Override
    public void checkVert(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL+ApiConstant.CHECK_VERTIFY_CODE)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())){
                            mView.onCheckVertSuccess();
                        } else {
                            mView.onGetVertFailed(response.body().getMessage());
                        }
                    }
                });
    }
}
